package com.kaiyidiannao.model.req.model;

import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.bean.TourBean;
import com.kaiyidiannao.model.req.base.BaseCommand;
import com.taplinker.core.util.json.JsonUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 *  凑队去-列表 异步请求d
 */
public class GetTourLIst extends BaseCommand {

    // 排序方式：费用、人气 (如果order缺省按添加时间排序,  如果order为1则按费用升序排列, 如果order为2则按人气降序排列, -1 :为费用的降序
    public static final int ORDER_DEFAULT_DIS= -1;
    public static final int ORDER_DEFAULT_COURSE= 1;
    public static final int ORDER_DEFAULT_POPULARITY = 2;
    //类型：长途、短途 (如果kind缺省为空则查询短途,如果kind为1则查询长途)
    public static final String KIND_DEFAULT_LONG = 1 + "";

    private int page =  DataCenter.LISTVIEW_DEFAULT_PAGE; //当前页码
    //旅游路线推荐列表页
    private int catid; //当前分类编号
    private String kind = -1 + ""; //类型：长途、短途
    private int  order = -1;//排序方式：费用、人气

    private String TAG = "GetTourLIst";

    /**
     * 短途、按添加时间排序
     * @param catid
     */
    public GetTourLIst(int catid, int page) {
        this.catid = catid;
        this.page = page;
    }

    /**
     * 长途、按添加时间排序
     * @param catid
     * @param kind
     */
    public GetTourLIst(int catid, String kind, int page) {
        this.catid = catid;
        this.kind = kind;
        this.page = page;
    }

    /**
     * 按费用升序,降序排列
     * @param order order = 1(按费用升序排列), order = 2 (按人气降序排列)
     */
    public GetTourLIst(int catid, int order, int page) {
        this.catid = catid;
        this.order = order;
        this.page = page;
    }

    @Override
    public String getRequestUrl() {
        return DataCenter.GET_TOUT_LIST_ADDRESS;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        paramters.put("catid", catid  + "");
        if(!this.kind.equals("-1")) {
            paramters.put("kind", kind  + "");
        }
        if(this.order != -1) {
            paramters.put("order", order  + "");
        }
        paramters.put("page",page + "");
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.getByHttpClient(url, paramters);
    }


    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "GetCaseComment.yes()");

        JSONArray jsonArray = null;
        try {
            Log.d(TAG, result.object.toString());

            JSONObject jsonObject = new JSONObject(result.object.toString());
            jsonArray = jsonObject.optJSONArray("data");

            ArrayList<TourBean> targetList = DataCenter.tourList;
            if (page == DataCenter.LISTVIEW_DEFAULT_PAGE) {
                targetList.clear();
            }
            if (page == DataCenter.PAGE) {
                for (int i = DataCenter.tourList.size()-1; i >= (DataCenter.gather_position/10+1)*10-10; i--) {
                    DataCenter.tourList.remove(i);
                }
            }

            for(int i = 0; i < jsonArray.length(); ++i){ // i从0开始，屏蔽提示信息
                JSONObject o = jsonArray.getJSONObject(i);
                TourBean bean = JsonUtil.jsonToObject(o.toString(), TourBean.class);
                targetList.add(bean);
            }
            Log.d(TAG, targetList.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        DataCenter.getInstance().callBacks(DataCenter.flag.GET_DAY_DAY_COUPON_LIST_SUCCESS);

        if (jsonArray != null && jsonArray.length() < 1) {
            DataCenter.getInstance().callBacks(DataCenter.flag.GET_DAY_DAY_COUPON_LIST_NO_MORE_DATA);
        }
    }

    @Override
    public void no(Result result) {
        super.no(result);
        Log.d(DataCenter.LOG_ERROR, "GetDayDayCouponLIst.no()");

        DataCenter.daydayCouponBeanList.clear();
        DataCenter.getInstance().callBacks(DataCenter.flag.GET_DAY_DAY_COUPON_LIST_FAILED);

    }
}
